<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="../vue.js"></script>
</head>

<body>
  <div id="app">
    <h2>书籍的总价格为 {{totalPrice}}</h2>
  </div>
  <script>
    const app = new Vue({
      el: "#app",
      data: {
        books: [{
            id: 123,
            name: "新华字典",
            price: 123
          },
          {
            id: 121233,
            name: "你不知道的JavaScript",
            price: 123
          },
          {
            id: 122133,
            name: "javaScript高级程序设计",
            price: 113
          },
          {
            id: 12543,
            name: "深入理解计算机原理",
            price: 223
          },
          {
            id: 121233,
            name: "新华字典",
            price: 93
          }
        ]
      },
      computed: {
        totalPrice: function () {
          let result = 0;
          // 原始写法
          for (let i = 0; i < this.books.length; i++) {
            result += this.books[i].price;
          }
          // ES6 写法
          /* for (let i in this.books) {
            result += this.books[i].price;
          } */
          return result;
        }
      }
    })
  </script>
</body>

</html>